import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import { Configuration } from '@ohos.app.ability.Configuration';

export default class EntryAbility extends UIAbility {windowStage_: window.WindowStage;


  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    AppStorage.SetOrCreate('currentColorMode', this.context.config.colorMode);
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }
  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });

  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
  // EntryAbility.ts入口文件中获取环境变化，修改状态
  onConfigurationUpdate(newConfig: Configuration): void {
    AppStorage.SetOrCreate('currentColorMode', newConfig.colorMode);
  }
  // onWindowStageCreate(windowStage) {
  //   // 1.创建悬浮窗。
  //   let windowClass = null;
  //   let config = {name: "floatWindow", windowType: window.WindowType.TYPE_FLOAT, ctx: this.context};
  //   window.createWindow(config, (err, data) => {
  //     if (err.code) {
  //       console.error('Failed to create the floatWindow. Cause: ' + JSON.stringify(err));
  //       return;
  //     }
  //     console.info('Succeeded in creating the floatWindow. Data: ' + JSON.stringify(data));
  //     windowClass = data;
  //     // 2.悬浮窗窗口创建成功后，设置悬浮窗的位置、大小及相关属性等。
  //     windowClass.moveWindowTo(300, 300, (err) => {
  //       if (err.code) {
  //         console.error('Failed to move the window. Cause:' + JSON.stringify(err));
  //         return;
  //       }
  //       console.info('Succeeded in moving the window.');
  //     });
  //     windowClass.resize(500, 500, (err) => {
  //       if (err.code) {
  //         console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
  //         return;
  //       }
  //       console.info('Succeeded in changing the window size.');
  //     });
  //     // 3.为悬浮窗加载对应的目标页面。
  //     windowClass.setUIContent("pages/MainPage", (err) => {
  //       if (err.code) {
  //         console.error('Failed to load the content. Cause:' + JSON.stringify(err));
  //         return;
  //       }
  //       console.info('Succeeded in loading the content.');
  //       // 3.显示悬浮窗。
  //       windowClass.showWindow((err) => {
  //         if (err.code) {
  //           console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
  //           return;
  //         }
  //         console.info('Succeeded in showing the window.');
  //       });
  //     });
  //     // 4.销毁悬浮窗。当不再需要悬浮窗时，可根据具体实现逻辑，使用destroy对其进行销毁。
  //     windowClass.destroyWindow((err) => {
  //       if (err.code) {
  //         console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err));
  //         return;
  //       }
  //       console.info('Succeeded in destroying the window.');
  //     });
  //   });
  // }
}
